package Ecu911_Interfaz;

import Ecu911_Excepciones.CedulaCorrecta;
import Ecu911_Excepciones.ExceptionSql;
import Ecu911_Excepciones.ValoresNulos;
import Ecu911_Negocio.NegUsuarios;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Random;
import java.util.TimerTask;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


import Ecu911_Negocio_Patrones.ClaseSingleton;

/**
 *
 * @author Marcelo Montaño
 */
public class FrmMantenimientoUsuarios extends javax.swing.JDialog {

    boolean mensajeAdvertenciaActivo;
    boolean mensajeErrorActivo;
    NegUsuarios miUsuario;
      
    /**
     * Creates new form FrmMantenimientoUsuarios
     */
    public FrmMantenimientoUsuarios(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.getContentPane().setBackground(Color.white);
        this.mensajeAdvertenciaActivo = false;
        this.mensajeErrorActivo = false;
        this.soloLetras(this.txtApellidos);
        this.soloLetras(this.txtNombres);
        this.soloNumeros(this.txtCedula);
        this.soloNumeros(this.txtTelefono);   
        FrmPrincipal.historialUsuario = ClaseSingleton.getmiClaseSingleton().getContenido();
        ClaseSingleton.getmiClaseSingleton().setContenidos(FrmPrincipal.historialUsuario);
    }

    private void estadoTextosInicioSistema(){
        this.txtApellidos.setEditable(false);
        this.txtCedula.setEditable(false);
        this.txtContraseña.setEditable(false);
        this.txtCorreo.setEditable(false);
        this.txtDireccion.setEditable(false);
        this.txtNombres.setEditable(false);
        this.txtTelefono.setEditable(false);
    }
    
    private void estadoTextosClickBotonNuevo(){
        this.mensajeAdvertenciaVisibleOn();
        this.txtApellidos.setEditable(true);
        this.txtCedula.setEditable(true);
        this.txtContraseña.setEditable(true);
        this.txtCorreo.setEditable(true);
        this.txtDireccion.setEditable(true);
        this.txtNombres.setEditable(true);
        this.txtTelefono.setEditable(true);
    }
    
    private void estadoTextosClickBotonEditar(){
        this.mensajeAdvertenciaVisibleOn();
        this.txtApellidos.setEditable(true);
    }
    
    private void estadoTextosClickBotonGuardar(){
        this.estadoTextosInicioSistema();
        this.limpiarTextos();
    }
    
    private void estadoTextosClickBotonCancelar(){
        this.estadoTextosClickBotonGuardar();
        this.mensajeAdvertenciaActivo = false;
        this.dispose();
    }
    
    private void estadoTextosClickBotonEliminar(){
        this.mensajeAdvertenciaVisibleOn();
        this.estadoTextosInicioSistema();
        this.limpiarTextos();
    }
    
    private void limpiarTextos(){
        this.txtApellidos.setText("");
        this.txtCedula.setText("");
        this.txtContraseña.setText("");
        this.txtCorreo.setText("");
        this.txtDireccion.setText("");
        this.txtNombres.setText("");
        this.txtTelefono.setText("");
    }
    
    private void mensajeAdvertenciaVisibleOn(){
        this.jLabel9.setVisible(false);
    }
    
    private void mensajeAdvertenciaVisibleOff(){
        this.jLabel9.setVisible(true);
    }
    
    private void soloNumeros(JTextField a){
        a.addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e){
                char c = e.getKeyChar();
                if(Character.isLetter(c)){
                    getToolkit().beep();
                    e.consume();
                }
            }
        }
                );
        
    }
    
    private void soloLetras(JTextField a){
        a.addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e){
                char c = e.getKeyChar();
                if(Character.isDigit(c)){
                    getToolkit().beep();
                    e.consume();
                }
            }
        }
                );
        
    }
    
    private boolean validarCamposTextoCompleto(){
       boolean check = true;
       if(this.txtCedula.getText().compareTo("")==0 ||
          this.txtNombres.getText().compareTo("")==0 ||
          this.txtApellidos.getText().compareTo("")==0 ||
          this.txtDireccion.getText().compareTo("")==0 ||
          this.txtTelefono.getText().compareTo("")==0 ||
          this.txtContraseña.getPassword().length==0){
           check = false;
       }
       return check;
   }
    
   private void pintarTextosVacios(){
       if(this.txtCedula.getText().compareTo("")==0){
           this.txtCedula.setBackground(Color.yellow);
       }
       else{
           this.txtCedula.setBackground(Color.white);
       }
       if(this.txtNombres.getText().compareTo("")==0){
           this.txtNombres.setBackground(Color.yellow);
       }
       else{
           this.txtNombres.setBackground(Color.white);
       }
       if(this.txtApellidos.getText().compareTo("")==0){
           this.txtApellidos.setBackground(Color.yellow);
       }
       else{
           this.txtApellidos.setBackground(Color.white);
       }
       if(this.txtTelefono.getText().compareTo("")==0){
           this.txtTelefono.setBackground(Color.yellow);
       }
       else{
           this.txtTelefono.setBackground(Color.white);
       }
       if(this.txtCorreo.getText().compareTo("")==0){
           this.txtCorreo.setBackground(Color.yellow);
       }
       else{
           this.txtCorreo.setBackground(Color.white);
       }
       if (txtDireccion.getText().isEmpty()){
           this.txtDireccion.setBackground(Color.yellow);
       }
       else{
           this.txtDireccion.setBackground(Color.white);
       }
       if(this.txtContraseña.getPassword().length==0){
           this.txtContraseña.setBackground(Color.yellow);
       }
       else{
           this.txtContraseña.setBackground(Color.white);
             
       } 
   } 
    
   public class automatico extends Observable{
        public automatico(){
             java.util.Timer timer = new java.util.Timer();
             timer.scheduleAtFixedRate(timerTask, 10,300);
        }
        TimerTask timerTask = new TimerTask(){
             public void run() {
                        Color c = Color.RED;
                        Random rand = new Random();
                        int x = rand.nextInt(3) + 1;
                        switch (x) {
                            case 1 : c = Color.white;break;
                                                            
                        }
                        jLabel9.setForeground(c);
                        
                    }
        };
   }   
  
   private boolean validarCedulaUsuario(String cedula){
       if(cedula.length()==10){
           boolean validacion = false, band = true;
           int suma = 0;
           int multiplicador = 2;
           ArrayList numeroDescompuesto = this.descomponerCadena(cedula);
           for(int i=0; i<numeroDescompuesto.size()-1; i++){
               int multiplicacion = (multiplicador * Integer.parseInt(numeroDescompuesto.get(i).toString()));
               if (multiplicacion>9){
                   multiplicacion = multiplicacion - 9;
               }
               suma = suma + multiplicacion;
               if (band){
                   multiplicador = 1;
                   band = false;
               }
               else{
                   multiplicador = 2;
                   band = true;
               }
           }
           suma = 10 - suma%10;
           int digitoPrueba = Integer.parseInt(numeroDescompuesto.get(9).toString());
           if (suma == digitoPrueba){
               validacion = true;
           }
           System.out.println(validacion);
           return validacion;
       }
       else{
           return false;
       }
   }
   
   private ArrayList descomponerCadena(String cadena){
       ArrayList cad = new ArrayList();
       for(int i=0; i<cadena.length(); i++){
           cad.add(cadena.substring(i, i+1));
       }
       return cad;
   }
   
   private void imprimirMensajeError(String msj){
       JOptionPane.showConfirmDialog(null,msj,
       "ERROR DE SISTEMA",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE,null); 
   }
   
   private void imprimirMensajeAcciónRealizada(String msj){
       JOptionPane.showConfirmDialog(null,msj,
       "ACCION DE SISTEMA",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null); 
   }
   
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCedula = new javax.swing.JTextField();
        txtNombres = new javax.swing.JTextField();
        txtApellidos = new javax.swing.JTextField();
        txtTelefono = new javax.swing.JTextField();
        txtCorreo = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDireccion = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        txtContraseña = new javax.swing.JPasswordField();
        jButton3 = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnNuevoUsuario = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ECU911 - [Mantenimiento Usuarios]");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Información usuario del sistema."));

        jLabel1.setText("Cédula/ID:");

        jLabel2.setText("Nombres:");

        jLabel3.setText("Apellidos:");

        jLabel4.setText("Dirección:");

        jLabel5.setText("Teléfono/Móvil:");

        jLabel6.setText("Correo:");

        txtCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCedulaKeyPressed(evt);
            }
        });

        txtDireccion.setColumns(20);
        txtDireccion.setRows(5);
        jScrollPane1.setViewportView(txtDireccion);

        jLabel7.setText("Contraseña:");

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/Cancelar2.png"))); // NOI18N
        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/Guardar.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/BuscarUsuario.png"))); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel9.setForeground(new java.awt.Color(255, 204, 51));
        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/MensajeAdvertencia.png"))); // NOI18N
        jLabel9.setText("[Seleccionar opción...]");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(0, 182, Short.MAX_VALUE)
                                .addComponent(btnGuardar)
                                .addGap(18, 18, 18)
                                .addComponent(jButton3))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(txtNombres)
                                        .addComponent(txtApellidos)
                                        .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(txtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(18, 18, 18)
                                            .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(txtCorreo)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE))
                                    .addComponent(txtContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(0, 350, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(btnGuardar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(jLabel9)
                .addGap(47, 47, 47))
        );

        jPanel2.setBackground(new java.awt.Color(255, 102, 0));

        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Información genaral de los usuarios del Sistema ECU911.");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel8)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addComponent(jLabel8)
                .addContainerGap())
        );

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(0, 0, 51));
        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/FlechaAbajo.png"))); // NOI18N
        jLabel10.setText("Opciones       ");
        jLabel10.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel10.setBorder(new javax.swing.border.SoftBevelBorder(0));
        jLabel10.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jLabel10.setRequestFocusEnabled(false);
        jLabel10.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnNuevoUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/NuevoUsuario.png"))); // NOI18N
        btnNuevoUsuario.setText("Nuevo");
        btnNuevoUsuario.setToolTipText("Registrar nuevo usuario");
        btnNuevoUsuario.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNuevoUsuario.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        btnNuevoUsuario.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNuevoUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoUsuarioActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/EditarUsuario.png"))); // NOI18N
        jButton1.setText("Editar");
        jButton1.setToolTipText("Editar datos usuario");
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/EliminarUsuario.png"))); // NOI18N
        jButton2.setText("Eliminar");
        jButton2.setToolTipText("Eliminar usuario");
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnNuevoUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator1)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator2)
                    .addComponent(jSeparator3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNuevoUsuario)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-685)/2, (screenSize.height-582)/2, 685, 582);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        this.mensajeAdvertenciaVisibleOn();
        new automatico();
        this.estadoTextosInicioSistema();
    }//GEN-LAST:event_formWindowOpened

    private void txtCedulaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCedulaKeyPressed
        if (!this.mensajeAdvertenciaActivo){
            this.mensajeAdvertenciaVisibleOff();
        }      
    }//GEN-LAST:event_txtCedulaKeyPressed

    private void btnNuevoUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoUsuarioActionPerformed
        FrmPrincipal.historialUsuario = FrmPrincipal.historialUsuario + "\nINGRESO OPCION NUEVO USUARIO";
        ClaseSingleton.getmiClaseSingleton().setContenidos(FrmPrincipal.historialUsuario);
        this.estadoTextosClickBotonNuevo();
        this.mensajeAdvertenciaActivo = true;
    }//GEN-LAST:event_btnNuevoUsuarioActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        FrmPrincipal.historialUsuario = FrmPrincipal.historialUsuario + "\nCANCELO MENU MANTENIMIENTO USUARIOS.";
        ClaseSingleton.getmiClaseSingleton().setContenidos(FrmPrincipal.historialUsuario);
        this.estadoTextosClickBotonCancelar();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void validarInformaciónNuevoRegistro(boolean val)throws ValoresNulos, CedulaCorrecta, ExceptionSql, SQLException{
        if (val){
            this.validarCedulaNuevoRegistro(this.validarCedulaUsuario(this.txtCedula.getText()));
        }
        else{
            this.pintarTextosVacios();
            throw new ValoresNulos();
        }
    }
    
    private void validarCedulaNuevoRegistro(boolean val)throws CedulaCorrecta, ExceptionSql, SQLException{
       if(val){
           this.guardarNuevoRegistro();
       }
       else{
           throw new CedulaCorrecta();
       }
    }
    
    private void guardarNuevoRegistro()throws ExceptionSql, SQLException{
        try{
            String password = new String(txtContraseña.getPassword());
            miUsuario = new NegUsuarios(this.txtCedula.getText(), 
                                        this.txtNombres.getText().toUpperCase(), 
                                        this.txtApellidos.getText().toUpperCase(), 
                                        this.txtDireccion.getText().toUpperCase(), 
                                        this.txtTelefono.getText(), 
                                        this.txtCorreo.getText(), 
                                        password);
            miUsuario.guardarMiUsuarioDB();
            this.imprimirMensajeAcciónRealizada("Información correcta. \n Registro almacenado.");
            this.estadoTextosClickBotonGuardar();
        }
        catch(ExceptionSql e){
            throw new ExceptionSql("Error en la base de datos. \nNo se pudo almacenar información.");
        }
    }
    
    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        FrmPrincipal.historialUsuario = FrmPrincipal.historialUsuario + "\nSELECCIONO OPCION GUARDAR";
        ClaseSingleton.getmiClaseSingleton().setContenidos(FrmPrincipal.historialUsuario);
        try{
            this.validarInformaciónNuevoRegistro(this.validarCamposTextoCompleto());
        } 
        catch(ValoresNulos e){
            this.imprimirMensajeError(e.getMessage());
        }
        catch(CedulaCorrecta e){
            this.imprimirMensajeError(e.getMessage());
        }
        catch(SQLException e){
            this.imprimirMensajeError(e.getMessage());
        }         
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.estadoTextosClickBotonEditar();
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jButton5ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmMantenimientoUsuarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmMantenimientoUsuarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmMantenimientoUsuarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmMantenimientoUsuarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmMantenimientoUsuarios dialog = new FrmMantenimientoUsuarios(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNuevoUsuario;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JPasswordField txtContraseña;
    private javax.swing.JTextField txtCorreo;
    private javax.swing.JTextArea txtDireccion;
    private javax.swing.JTextField txtNombres;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables
}
